/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.junit;

import org.junit.Test;
import redora.exceptions.RedoraException;
import redora.test.rdo.businessrules.JUnitMasterBusinessRules;
import redora.test.rdo.businessrules.JUnitNotNullChildBusinessRules;
import redora.test.rdo.model.JUnitChild;
import redora.test.rdo.model.JUnitMaster;
import redora.test.rdo.model.JUnitNotNullChild;
import redora.test.rdo.model.JUnitNtoM;

import static redora.service.BusinessRuleViolation.Action.Insert;
import static redora.service.BusinessRuleViolation.StandardRule.*;
import static redora.util.JUnitUtil.assertRedoraPersist;

/**
 * TrashTest will check if the trash can functionality works as wished.
 * 
 * @author Nanjing RedOrange (www.red-orange.cn)
 */
public class BusinessRules {

    /**
     * Check the NotNull property: insert an nullified, and update an empty
     * string.
     */
    @Test
    public void notNull() throws RedoraException {
        JUnitMaster master = new JUnitMaster();
        // This should not fire any rules, preparing for update check
        master.avoidNull();
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert));

        // This should fire all rules
        master = new JUnitMaster();
        assertRedoraPersist( "null all", JUnitMasterBusinessRules.check(master, Insert), NotNull);

        // Check string notnull
        master = new JUnitMaster();
        master.avoidNull();
        //noinspection ConstantConditions
        master.setNotnull(null);
        assertRedoraPersist("null string", JUnitMasterBusinessRules.check(master, Insert), NotNull);

        // Check string empty
        master = new JUnitMaster();
        master.avoidNull();
        master.setNotnull("");
        assertRedoraPersist("empty string", JUnitMasterBusinessRules.check(master, Insert), NotNull);

        // Check string empty space
        master = new JUnitMaster();
        master.avoidNull();
        master.setNotnull("   ");
        assertRedoraPersist("empty space string", JUnitMasterBusinessRules.check(master, Insert), NotNull);

        // Check integer empty
        master = new JUnitMaster();
        master.avoidNull();
        //noinspection ConstantConditions
        master.setIntegerNotNull(null);
        assertRedoraPersist("null integer", JUnitMasterBusinessRules.check(master, Insert), NotNull);

        // Check boolean empty
        master = new JUnitMaster();
        master.avoidNull();
        //noinspection ConstantConditions
        master.setBooleanNotNull(null);
        assertRedoraPersist("boolean integer", JUnitMasterBusinessRules.check(master, Insert), NotNull);
    }

    @Test
    public void notNullObject()  throws RedoraException {
        JUnitNotNullChild child = new JUnitNotNullChild();
        child.avoidNull();
        //noinspection ConstantConditions
        child.setJUnitMaster(null);
        assertRedoraPersist("null integer", JUnitNotNullChildBusinessRules.check(child, Insert), NotNull);
    }

    /**
     * Check on MaxLength business rule
     */
    @Test
    public void maxLength() throws RedoraException {
        JUnitMaster master = new JUnitMaster();
        master.setSstring("zxcvbnmasdfghjklqwertyuiopzxcvbnmasdfhjklqwertyuiopzxcvbnmasdf" +
                "ghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopqwertyuiopasdfghjklzxcvbnmzxcvb" +
                "nmasdfghjklqwertyuiopasdfghjklzxcvbnmasdfghjklqwertyuiopzxcvbnmasdfghjkl" +
                "qwertyuiopzxcvbnmasdfghjklqwertyuiopasdfghjkzxcvbnmasd");
        master.avoidNull();
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert), MaxLength);
    }

    /**
     * when Regexp "redorange", check if the regexp business rule is working
     * (all descriptions should have redorange).
     */
    @Test
    public void regexp() throws RedoraException {
        JUnitMaster master = new JUnitMaster();
        master.avoidNull();
        master.setRregexp("Regexp OK, redorange");
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert));

        master.setRregexp("RegExp Test NOT OK");
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert), MatchRegexp);
    }

    @Test
    public void child() throws RedoraException {
        JUnitMaster master = new JUnitMaster();
        master.avoidNull();
        JUnitChild child = master.getJUnitChildren().insert(new JUnitChild());
        child.setName("This is longer than 50, really, count it if you don't believe.");
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert), MaxLength);
    }

    //@Test NOT WORKING
    //TODO fix
    public void childNtoM() throws RedoraException {
        JUnitMaster master = new JUnitMaster();
        master.avoidNull();
        JUnitNtoM child = master.getJUnitNtoMs().insert(new JUnitNtoM());
        child.setName("This is longer than 10");
        assertRedoraPersist(JUnitMasterBusinessRules.check(master, Insert), MaxLength);
    }
}
